package edu.java.homework.hw03.ipj07;

import java.util.Scanner;

/**
 * Check if a matrix from characters is symmetric in according to the main
 * diagonal
 * 
 * @author Tsvetan Vasilev
 * 
 */
public class Exer06 {

    public static final int DIMENTION=9;
    
    public static void main(String[] args) {
	Scanner input = new Scanner(System.in);

	char[][] strMatrix = new char[DIMENTION][DIMENTION];
	// input the series of strings
	inputStrMatrix(strMatrix);
	
	// print matrix
	System.out.println();
	printStrMatrix(strMatrix);

	// check the symmetric
	boolean isSymmetric = isSymmetricMatrix(strMatrix);
	System.out.println("\nThe matrix is symmetric? " + isSymmetric);
	input.close();
    }

    public static boolean isSymmetricMatrix(char[][] strMatrix) {
	if (strMatrix == null) {
	    throw new IllegalArgumentException("Null-pointed array");
	}

	boolean isSymmetric = true;
	int length = strMatrix.length;
	wrapperLoop: for (int i = 1; i < length; i++) {
	    for (int j = 0; j < strMatrix[i].length; j++) {
		if (i == j) {
		    break;
		}
		if (strMatrix[i][j] != strMatrix[j][i]) {
		    isSymmetric = false;
		    break wrapperLoop;
		}
	    }
	}

	return isSymmetric;
    }
    
    public static void inputStrMatrix(char[][] strMatrix){
	if (strMatrix == null) {
	    throw new IllegalArgumentException("Null-pointed array");
	}
	
	System.out
	.println("Please, enter the strings. The max number of strings is 9.");
	Scanner input=new Scanner(System.in);
	int length=strMatrix.length;
        for (int i = 0; i < length; i++) {
            System.out.print("String[" + i + "]= ");
            String temp = input.nextLine();
            int strLength=temp.length();
            if (strLength > 9) {
        	throw new IllegalArgumentException(
        		"You've entered a string with more than 9 characters.");
            }
            if(strLength<9){
        	StringBuffer sb= new StringBuffer(length);
        	sb.append(temp);
        	for(int j=strLength; j<length;j++){
        	    sb.append(' ');
        	}
        	temp=sb.toString();
            }
            strMatrix[i] = temp.toCharArray();
        }
        
        input.close();
    }

    public static void printStrMatrix(char[][] strMatrix) {
	if (strMatrix == null) {
	    throw new IllegalArgumentException("Null-pointed array");
	}

	int length = strMatrix.length;
	// first way
	for (int i = 0; i < length; i++) {
	    System.out.println(strMatrix[i]);
	}

	// second way
	// for(int i=0;i<length;i++){
	// for(int j=0;j<strMatrix[i].length;j++){
	// System.out.print(strMatrix[i][j]);
	// }
	// System.out.println();
	// }
    }
}
